package com.cmcc.aregister.cs.util;

import java.util.HashMap;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import com.cmcc.aregister.cs.cache.AutoAppointTipCache;
import com.cmcc.aregister.cs.cache.Cache;
import com.cmcc.aregister.cs.cache.HaoyuanTipCache;

/**
 * 
 * @author swq
 * 
 */
public class DBUtils {
	private static final MyLogger logger = MyLogger.getLogger();
	private DatabaseHelper mDatabaseHelper;
	private Context context;
	private SQLiteDatabase db;
	private static final String CACHE_DB_NAME = "wifi_city";
	private int DB_VERSION = 3;

	public DBUtils(Context app) {
		logger.e("DB_VERSION:" + DB_VERSION);
		this.context = app;
		Map<String, String> sqls = new HashMap<String, String>();
		sqls.put(Cache.TABLE_NAME, Cache.SQLNAME);
		sqls.put(AutoAppointTipCache.TABLE_NAME, AutoAppointTipCache.SQLNAME);
		sqls.put(HaoyuanTipCache.TABLE_NAME, HaoyuanTipCache.SQLNAME);
		mDatabaseHelper = new DatabaseHelper(context, CACHE_DB_NAME, null,
				DB_VERSION, sqls);
	}

	
	/**
	 * 打开数据库
	 */
	public DBUtils open() throws SQLException {
		db = mDatabaseHelper.getWritableDatabase();
		return this;
	}

	/**
	 * 关闭数据库
	 */
	public void close() {
		mDatabaseHelper.close();
	}

	/**
	 * 插入数据
	 */
	public long insert(String tableName, ContentValues initialValues) {
		long i = db.insert(tableName, null, initialValues);
		return i;
	}

	/**
	 * 删除数据
	 */
	public boolean delete(String tableName, String whereClause) {
		boolean flag = db.delete(tableName, whereClause, null) > 0;
		return flag;
	}

	/**
	 * 更新数据
	 */
	public boolean update(String tableName, String whereClause,
			ContentValues args) {
		boolean flag = db.update(tableName, args, whereClause, null) > 0;
		return flag;
	}

	/**
	 * 查询数据
	 */
	public Cursor query(String tableName, String[] columns, String querySql) {
		Cursor mCursor = db.query(true, tableName, columns, querySql, null,
				null, null, null, null);

		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public Cursor queryAll(String tableName, String[] columns) {
		Cursor mCursor = db.query(true, tableName, columns, null, null, null,
				null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public Cursor queryAllByOrderBy(String tableName, String[] columns,
			String orderBy) {
		Cursor mCursor = db.query(true, tableName, columns, null, null, null,
				null, orderBy, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public Cursor queryAllByOrderByAndWhere(String tableName, String[] columns,
			String orderBy, String selection) {
		Cursor mCursor = db.query(true, tableName, columns, selection, null,
				null, null, orderBy, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	private class DatabaseHelper extends SQLiteOpenHelper {
		private Map<String, String> sqls = null;

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version, Map<String, String> sqls) {
			super(context, name, factory, version);
			this.sqls = sqls;
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			logger.d("----------database----------onCreate----------");
			if (sqls != null && sqls.size() > 0) {
				for (String table : sqls.values()) {
					db.execSQL(table);
				}
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			logger.d("----------database----------onUpgrade----------");
			logger.d("----------oldVersion----------" + oldVersion);
			logger.d("----------oldVersion----------" + newVersion);
			if (newVersion > oldVersion) {
				if (sqls != null && sqls.size() > 0) {
					for (String table : sqls.keySet()) {
						db.execSQL("DROP TABLE IF EXISTS  " + table);
					}
				}
				onCreate(db);
			}
		}
	}
}
